package com.wpzf.ndhc.service.impl;

import com.wpzf.ndhc.domain.*;
import com.wpzf.ndhc.utils.TableUtil;
import com.wpzf.ndhc.mapper.WpzfNdhcShijshMapper;
import com.wpzf.ndhc.mapper.WpzfNdhcXjtbTbBaseMapper;
import com.wpzf.ndhc.service.WpzfNdhcShijshService;
import com.wpzf.zxhc.domain.WpCjTbhcjgshij2023;
import com.wpzf.zxhc.domain.YtbTbInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class WpzfNdhcShijshServiceImpl implements WpzfNdhcShijshService {

    @Autowired
    WpzfNdhcShijshMapper wpzfNdhcShijshMapper;

    @Autowired
    WpzfNdhcXjtbTbBaseMapper wpzfNdhcXjtbTbBaseMapper;

    @Override
    public List<Map> getCityDkShPch(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcShijshMapper.getCityDkShPch(param);
    }

    @Override
    public List<Map> getCityDkShInfo(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcShijshMapper.getCityDkShInfo(param);
    }

    @Override
    public List<Map> getDkShInfoByXzq(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcShijshMapper.getDkShInfoByXzq(param);
    }

    @Override
    public List<Map> getDkShInfoByXzqIds(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcShijshMapper.getDkShInfoByXzqIds(param);
    }

    @Override
    public List<Map> getLeftData(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcShijshMapper.getLeftData(param);
    }

    @Override
    public WpCjDkxxbxjNdDTO getDkInfoByDkbh(Map<String, Object> map) {
        map = TableUtil.TableName(map);
        WpCjDkxxbxjNdDTO dkInfoByDkbh = wpzfNdhcShijshMapper.getDkInfoByDkbh(map);
        if ("符合法律法规和相关政策用地".equals(dkInfoByDkbh.getPdlx())) {
        }
        return dkInfoByDkbh;
    }

    @Override
    public List<WpCjDkxxbxjNdDTO> getRelationDkInfoByJcbh(Map<String, Object> map) {
        map = TableUtil.TableName(map);
        return wpzfNdhcShijshMapper.getRelationDkInfoByJcbh(map);
    }

    @Override
    public List<WpCjTbxxbxjNdDTO> getTbInfoByJcbh(Map<String, Object> map) {
        map = TableUtil.TableName(map);
        return wpzfNdhcShijshMapper.getTbInfoByJcbh(map);
    }

    @Override
    public Map getCityDkHcJgInfo(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcShijshMapper.getCityDkHcJgInfo(param);
    }

    @Override
    public Integer deleteDkHcJg(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcShijshMapper.deleteDkHcJg(param);
    }

    @Override
    public Integer selectDkHcJg(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcShijshMapper.selectDkHcJg(param);
    }

    @Override
    public Integer updateDkHcJg(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcShijshMapper.updateDkHcJg(param);
    }

    @Override
    public Integer insertDkHcJg(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcShijshMapper.insertDkHcJg(param);
    }

    @Override
    public Integer insertOperationFlow(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcShijshMapper.insertOperationFlow(param);
    }

    @Override
    public Integer updateDkzt(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcShijshMapper.updateDkzt(param);
    }

    @Override
    public Integer returnDkHcXj(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        String sDkbh = param.get("dkbh") + "";
        String[] arrDkbh = sDkbh.split(",");
        Integer nSuccess = 0;
        for (String dkbh : arrDkbh) {
            param.replace("dkbh", dkbh);
            nSuccess += wpzfNdhcShijshMapper.updateDkzt(param);
        }
        return nSuccess;
    }

    @Override
    public Integer citySubmit(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        /**
         * 是否有未通过数据
         * 更新流转记录
         * 更新地块状态*/
        param.put("dkzt", "3");
        Integer nBtg = wpzfNdhcShijshMapper.getBtgDkCount(param);
        Integer nInsert = -1;
        Integer nUpDkzt = -1;
        Integer nUpSftj = -1;
        if (nBtg == 0) {
            nInsert = wpzfNdhcShijshMapper.insertOperationFlow(param);
            nUpSftj = wpzfNdhcShijshMapper.updateSftjByXzq(param);
            nUpDkzt = wpzfNdhcShijshMapper.updateDkztByXzq(param);
        }
        return nUpDkzt;
    }

    @Override
    public List<Map> getProvBackPch(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcShijshMapper.getProvBackPch(param);
    }

    @Override
    public List<Map> getProvBackDkInfo(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcShijshMapper.getProvBackDkInfo(param);
    }

    @Override
    public List<Map> getDkFlowInfo(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcShijshMapper.getDkFlowInfo(param);
    }

    @Override
    public List<Map> getXjZgDkInfo(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcShijshMapper.getXjZgDkInfo(param);
    }

    @Override
    public List<Map> getXjZgDkInfoIds(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcShijshMapper.getXjZgDkInfoIds(param);
    }

    @Override
    public List<Map> getXjZgDkPch(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcShijshMapper.getXjZgDkPch(param);
    }

    @Override
    public List<Map> getXjZgDkLeftData(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcShijshMapper.getXjZgDkLeftData(param);
    }

    @Override
    public Map getDkHcJgInfoXjZg(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        Map map = wpzfNdhcShijshMapper.getCityDkHcJgInfo(param);
        if (map == null)
            return map;
        if (map.containsKey("ecsfhc")) {
            if ((map.get("ecsfhc") + "").isEmpty() || map.get("ecsfhc").equals("")) {
                map.replace("hcjl", "");
                map.replace("hcsm", "");
            }
        } else {
            map.replace("hcjl", "");
            map.replace("hcsm", "");
        }
        return map;
    }

    @Override
    public Integer updateDkHcJgXjZg(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcShijshMapper.updateDkHcJgXjZg(param);
    }

    @Override
    public Integer updateDkHcJgTjByXjZg(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcShijshMapper.updateDkHcJgTjByXjZg(param);
    }

    @Override
    public Integer xjZgSubmit(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        /**
         * 是否有未通过数据
         * 更新流转记录
         * 更新地块状态*/
        Integer nInsert = wpzfNdhcShijshMapper.insertOperationFlow(param);
        Integer nUpDkzt = wpzfNdhcShijshMapper.updateDkztByXjZg(param);
        Integer nUpSftj = wpzfNdhcShijshMapper.updateDkHcJgTjByXjZg(param);
        return nUpDkzt;
    }

    @Override
    public Integer saveShiTbjg(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        wpzfNdhcShijshMapper.deleteDkHcJg(param);
        param.put("hcsj", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((new Date()).getTime()));
        return wpzfNdhcShijshMapper.saveShiTbjg(param);
    }

    @Override
    public WpCjTbxxbxjNdDTO getTbxxbxjByJcbh(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcShijshMapper.getTbxxbxjByJcbh(param);
    }

    @Override
    public List<WpCjTbxxbxjNdDTO> getTbxxbxjByJcbhList(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcShijshMapper.getTbxxbxjByJcbhList(param);
    }

    @Override
    public WpCjTbhcjgshijNdDTO getTbhcjgshijByDkbh(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        return wpzfNdhcShijshMapper.getTbhcjgshijByDkbh(param);
    }

    @Override
    public Integer shiSubmit(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        Integer rest = 0;
        wpzfNdhcShijshMapper.deleteDkHcJg(param);
        param.put("hcsj", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((new Date()).getTime()));
        param.put("sftj", "是");
        param.put("tjsubmitdate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((new Date()).getTime()));
        rest = wpzfNdhcShijshMapper.shiSubmit(param);
//        param.put("dkzt", "3");
        wpzfNdhcXjtbTbBaseMapper.updateDkxxbDkzt(param);
        param.put("czsj", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((new Date()).getTime()));
        param.put("result", param.get("hcjl"));
        param.put("sm", param.get("hcsm"));
        param.put("userlevel", "市");
        wpzfNdhcXjtbTbBaseMapper.saveDkxxbxjlz(param);
        return rest;
    }

    @Override
    @Transactional
    public Integer shiThxg(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        Integer rest = 0;

        param.put("czsj", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((new Date()).getTime()));
        param.put("result", param.get("hcjl"));
        param.put("sm", param.get("hcsm"));
        param.put("userlevel", "市");
        wpzfNdhcXjtbTbBaseMapper.saveDkxxbxjlz(param);
        param.put("dkzt", "4");
        param.put("thsj", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((new Date()).getTime()));
        wpzfNdhcShijshMapper.updateDkzt(param);

        return rest;
    }

    @Override
    public List<WpzfNdhcYtbTbInfo> queryShiTask(Map<String, Object> parm) {
        parm = TableUtil.TableName(parm);
        return wpzfNdhcShijshMapper.queryShiTask(parm);
    }

    @Override
    public List<WpzfNdhcYtbTbInfo> queryShiTaskEX(Map<String, Object> parm) {
        parm = TableUtil.TableName(parm);
        List<WpzfNdhcYtbTbInfo> ytbTbInfoList = wpzfNdhcShijshMapper.queryShiTaskEX(parm);
        for (WpzfNdhcYtbTbInfo ytbTbInfo : ytbTbInfoList) {
            Map<String, Object> map = new HashMap<>();
            map.put("dkbh", ytbTbInfo.getDkbh());
            map.put("xzqdm", ytbTbInfo.getXzqdm());
            map.put("pch", ytbTbInfo.getPch());
            map.put("year", parm.get("year"));
            map = TableUtil.TableName(map);
            Map<String, Object> shiData = wpzfNdhcXjtbTbBaseMapper.queryShi(map);
            if (shiData != null) {
                ytbTbInfo.setShiHcjl(shiData.get("hcjl") + "");
                ytbTbInfo.setShiHcsm(shiData.get("hcsm") + "");
                ytbTbInfo.setShiUsername(shiData.get("username") + "");
                ytbTbInfo.setShiRealname(shiData.get("realname") + "");
            }

            Map<String, Object> shengData = wpzfNdhcXjtbTbBaseMapper.querySheng(map);
            if (shengData != null) {
                ytbTbInfo.setShengHcjl(shengData.get("hcjl") + "");
                ytbTbInfo.setShengHcsm(shengData.get("hcsm") + "");
                ytbTbInfo.setShengUsername(shengData.get("username") + "");
                ytbTbInfo.setShengRealname(shengData.get("realname") + "");
            }
        }
        return ytbTbInfoList;
    }

    @Override
    public List<WpzfNdhcYtbTbInfo> queryShiTaskZG(Map<String, Object> parm) {
        parm = TableUtil.TableName(parm);
        return wpzfNdhcShijshMapper.queryShiTaskZG(parm);
    }

    @Override
    public List<WpzfNdhcYtbTbInfo> queryShiTaskZGEX(Map<String, Object> parm) {
        parm = TableUtil.TableName(parm);
        return wpzfNdhcShijshMapper.queryShiTaskZGEX(parm);
    }

    @Override
    public Integer shiSubmitZG(Map<String, Object> param) {
        param = TableUtil.TableName(param);
        Integer rest = 0;
        wpzfNdhcShijshMapper.deleteDkHcJg(param);
        param.put("hcsj", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((new Date()).getTime()));
        param.put("sftj", "是");
        param.put("tjsubmitdate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((new Date()).getTime()));
        rest = wpzfNdhcShijshMapper.shiSubmit(param);
        param.put("dkzt", "6");
        wpzfNdhcXjtbTbBaseMapper.updateDkxxbDkzt(param);
        param.put("czsj", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((new Date()).getTime()));
        param.put("result", param.get("hcjl"));
        param.put("sm", param.get("hcsm"));
        param.put("userlevel", "市");
        wpzfNdhcXjtbTbBaseMapper.saveDkxxbxjlz(param);
        return rest;
    }

    @Override
    public List<Map<String, Object>> queryShiBtgdk(Map<String, Object> parm) {
        parm = TableUtil.TableName(parm);
        return wpzfNdhcShijshMapper.queryShiBtgdk(parm);
    }

    @Override
    public Integer queryShiBtgdkCount(Map<String, Object> parm) {
        parm = TableUtil.TableName(parm);
        return wpzfNdhcShijshMapper.queryShiBtgdkCount(parm);
    }


    @Override
    public List<Map<String, Object>> getPch(Map<String, Object> param) {
        return wpzfNdhcShijshMapper.getPch(param);
    }

    @Override
    public List<Map<String, Object>> getXian(Map<String, Object> param) {
        return wpzfNdhcShijshMapper.getXian(param);
    }

    @Override
    public List<Map<String, Object>> queryDkList(Map<String, Object> param) {
        return wpzfNdhcShijshMapper.queryDkList(param);
    }

    @Override
    public Integer queryDkListCount(Map<String, Object> param) {
        return wpzfNdhcShijshMapper.queryDkListCount(param);
    }

    @Override
    public Map<String, Object> queryDkByIndex(Map<String, Object> param) {
        return wpzfNdhcShijshMapper.queryDkByIndex(param);
    }

    @Override
    public Map<String, Object> queryDkInfoByDkbh(Map<String, Object> param) {
        return wpzfNdhcShijshMapper.queryDkInfoByDkbh(param);
    }

    @Override
    public List<WpSjHcfjNdDTO> querySjHcfjByDkbsm(Map<String, Object> param) {
        return wpzfNdhcShijshMapper.querySjHcfjByDkbsm(param);
    }

    @Override
    public Map<String, Object> queryTbInfo(Map<String, Object> param) {
        return wpzfNdhcShijshMapper.queryTbInfo(param);
    }

    @Override
    public Map<String, Object> queryDkshjgShijiByDkbh(Map<String, Object> param) {
        return wpzfNdhcShijshMapper.queryDkshjgShijiByDkbh(param);
    }

    @Override
    public List<Map<String, Object>> queryLzByDkbh(Map<String, Object> param) {
        return wpzfNdhcShijshMapper.queryLzByDkbh(param);
    }

    @Override
    public List<Map<String, Object>> queryRelevantDkByDkbh(Map<String, Object> param) {
        return wpzfNdhcShijshMapper.queryRelevantDkByDkbh(param);
    }

    @Override
    public List<WpSjsbDkpwjbbxxNdDTO> queryDkpwjbxxByDkbsm(Map<String, Object> param) {
        return wpzfNdhcShijshMapper.queryDkpwjbxxByDkbsm(param);
    }

    @Override
    public List<WpSjsbDkfjjbxxNdDTO> queryDkfjjbxxByPwtbbsm(WpSjsbDkpwjbbxxNdDTO wpSjsbDkpwjbbxxNdDTO) {
        return wpzfNdhcShijshMapper.queryDkfjjbxxByPwtbbsm(wpSjsbDkpwjbbxxNdDTO);
    }

    @Override
    public List<WpSjsbDkfjjbxxNdDTO> queryDkfjjbxxByDkbsm(Map<String, Object> param) {
        return wpzfNdhcShijshMapper.queryDkfjjbxxByDkbsm(param);
    }

    @Override
    @Transactional
    public Integer saveShiDkshjg(Map<String, Object> param) {
        wpzfNdhcShijshMapper.deleteShiDkshjg(param);
        param.put("shsj", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((new Date()).getTime()));
        return wpzfNdhcShijshMapper.saveShiDkshjg(param);
    }

    @Override
    @Transactional
    public Integer submitShiDkshjg(Map<String, Object> param) {
        Integer rest = 0;
        wpzfNdhcShijshMapper.deleteShiDkshjg(param);
        param.put("sftj", "是");
        param.put("shsj", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((new Date()).getTime()));
        rest = wpzfNdhcShijshMapper.saveShiDkshjg(param);

        param.put("dkzt", "3");
        wpzfNdhcShijshMapper.updateDkjbxxbDkzt(param);

        param.put("result", param.get("shjg"));
        param.put("sm", param.get("shsm"));
        param.put("level", "市");
        param.put("czsj", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((new Date()).getTime()));
        wpzfNdhcShijshMapper.saveDklz(param);
        return rest;
    }



}
